package com.ct.application.shipping.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ct.application.commons.vo.StatusRequest;
import com.ct.application.shipping.model.po.ChuanBoZhiWeiSavePO;
import com.ct.application.shipping.model.po.ChuanBoZhiWeiSearchPO;
import com.ct.application.shipping.model.vo.ChuanBoZhiWeiVO;
import com.ct.application.shipping.service.ChuanBoXinXiZhiWeiService;
import com.ct.application.shipping.service.ChuanBoZhiWeiService;
import com.ct.repository.commons.BaseServiceImpl;
import com.ct.shipping.mapper.ChuanBoZhiWeiMapper;
import com.ct.shipping.orm.ChuanBoXinXiDO;
import com.ct.shipping.orm.ChuanBoXinXiZhiWeiDO;
import com.ct.shipping.orm.ChuanBoZhiWeiDO;
import com.ct.utils.result.StatusEnums;
import com.ct.utils.threadlocal.UserThreadLocal;
import com.ct.utils.valid.ValidatorUtil;
import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 船舶职位 服务实现类
 * </p>
 *
 * @author carlo
 */
@Service
public class ChuanBoZhiWeiServiceImpl extends BaseServiceImpl<ChuanBoZhiWeiMapper, ChuanBoZhiWeiDO> implements ChuanBoZhiWeiService {


    @Resource
    private ChuanBoXinXiZhiWeiService chuanBoXinXiZhiWeiService;
    /**
     * 查询职位分页列表
     */
    @Override
    public List<ChuanBoZhiWeiVO> findChuanBoZhiWeisBiao(){
        LambdaQueryWrapper<ChuanBoZhiWeiDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ChuanBoZhiWeiDO::getZhuangTai, StatusEnums.STATUS.getCode());
        lambdaQueryWrapper.orderByAsc(ChuanBoZhiWeiDO::getXuHao);
        return tranferListBo(this.list(lambdaQueryWrapper),ChuanBoZhiWeiVO.class);
    }

    @Override
    public Page<ChuanBoZhiWeiVO> findChuanBoZhiWei(ChuanBoZhiWeiSearchPO searchPO) {
        LambdaQueryWrapper<ChuanBoZhiWeiDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Objects.nonNull(searchPO.getStatus()),ChuanBoZhiWeiDO::getZhuangTai,searchPO.getStatus());
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(searchPO.getKeyword()), ChuanBoZhiWeiDO::getZhiWeiMing,searchPO.getKeyword());
        lambdaQueryWrapper.orderByAsc(ChuanBoZhiWeiDO::getXuHao);
        Page<ChuanBoZhiWeiDO> page = new Page<>(searchPO.getPageNo(), searchPO.getPageSize());
        Page<ChuanBoZhiWeiDO> page1 = this.page(page, lambdaQueryWrapper);
        if(Objects.nonNull(searchPO.getXinxiId()) ) {
            Page<ChuanBoZhiWeiVO> chuanBoZhiWeiVOPage = tranferPageBo(page1, ChuanBoZhiWeiVO.class);
            List<ChuanBoZhiWeiVO> records = chuanBoZhiWeiVOPage.getRecords();
            if(CollectionUtils.isNotEmpty(records)){
                Map<Long,Integer> map = chuanBoXinXiZhiWeiService.listChuanBoZhiweiXin(searchPO.getXinxiId());
                if (Objects.nonNull(map)) {
                    for (ChuanBoZhiWeiVO record : records) {
                        if(Objects.nonNull(map.get(record.getId()))) {
                            record.setZhiweinum(map.get(record.getId()));
                        }
                    }
                }
            }
            chuanBoZhiWeiVOPage.setRecords(records);
            return chuanBoZhiWeiVOPage;
        }else{
            return tranferPageBo(page1, ChuanBoZhiWeiVO.class);
        }
    }


    /**
     * 保存职位
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveChuanBoZhiWei(ChuanBoZhiWeiSavePO savePO){
        savePO.setId(null); //新增不需要id
        if (StringUtils.isEmpty(savePO.getZhiWeiMing() )) {
            throw new RuntimeException("职位名称不能为空");
        }
        if (savePO.getXuHao() == null) {
            throw new RuntimeException("序号不能为空");
        }
        ChuanBoZhiWeiDO queryChuanBoXinXi = this.lambdaQuery()
                .eq(ChuanBoZhiWeiDO::getZhiWeiMing, savePO.getZhiWeiMing())
                .one();
        if (queryChuanBoXinXi != null) {
            throw new RuntimeException("《"+savePO.getZhiWeiMing()+"》职位名称已存在，请更换");
        }


        ChuanBoZhiWeiDO chuanBoZhiWeiDO = tranferBo(savePO, ChuanBoZhiWeiDO.class);
        if (Objects.nonNull(chuanBoZhiWeiDO)){
            chuanBoZhiWeiDO.setSystemId(UserThreadLocal.get().getSystemId());
            chuanBoZhiWeiDO.setSystemName(UserThreadLocal.get().getSystemName());
            return this.save(chuanBoZhiWeiDO);
        }
        return false;
    }

    /**
     * 修改职位
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateChuanBoZhiWei(ChuanBoZhiWeiSavePO savePO){
        if (savePO.getId() == null) {
            throw new RuntimeException("id不能为空");
        }
        if (StringUtils.isEmpty(savePO.getZhiWeiMing())) {
            throw new RuntimeException("职位名称不能为空");
        }
        if (savePO.getXuHao() == null) {
            throw new RuntimeException("序号不能为空");
        }
        ChuanBoZhiWeiDO queryChuanBoXinXi = this.lambdaQuery()
                .eq(ChuanBoZhiWeiDO::getZhiWeiMing, savePO.getZhiWeiMing())
                .ne(ChuanBoZhiWeiDO::getId, savePO.getId())
                .one();
        if (queryChuanBoXinXi != null) {
            throw new RuntimeException("《"+savePO.getZhiWeiMing()+"》职位名称已存在，请更换");
        }

        ChuanBoZhiWeiDO chuanBoZhiWeiDO = tranferBo(savePO, ChuanBoZhiWeiDO.class);
        if (Objects.nonNull(chuanBoZhiWeiDO)){
            return this.updateById(chuanBoZhiWeiDO);
        }
        return false;
    }


    /**
     * 删除职位
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean delChuanBoZhiWei(Long id){
        return this.removeById(id);
    }


    /**
     * 职位状态修改。
     * 0:停用、1:启用
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateStatus(StatusRequest statusRequest){
        if(ValidatorUtil.isNumeric(statusRequest.getValue())) {
            return this.updateStatus(statusRequest.getId(), statusRequest.getField(), Integer.parseInt(statusRequest.getValue()));
        }else{
            return this.updateStatusStr(statusRequest.getId(), statusRequest.getField(), statusRequest.getValue());
        }
    }


    @Override
    public ChuanBoZhiWeiVO getChuanBoZhiWei(Long id){
        ChuanBoZhiWeiDO chuanBoZhiWeiDO = getById(id);
        return tranferBo(chuanBoZhiWeiDO,ChuanBoZhiWeiVO.class);
    }
}
